#! /usr/env/bin python
import httplib
import json
import socket
import time
import urllib
import ranker
import hashlib
import datetime
import sqlite3
 
class goliath(object):
 
	def __init__(self, tag, interval, data = 'test.db'):
		self.since_id = None
		self.tag = tag
		self.interval = interval
		self.ranker= ranker.Ranker(database = data)
		self.database = data
 
	def search(self):
		c = httplib.HTTPConnection("search.twitter.com")
		params = {'q' : self.tag}
		if self.since_id is not None:
			#print "Set since_id to %d", (self.since_id)
			params['since_id'] = self.since_id
		params['result_type'] = "recent"
		params['rpp'] = 3
		params['page'] = 1
		path = "%s?%s" %("/search.json", urllib.urlencode(params))
		try:
			c.request('GET', path)
			data = c.getresponse().read()
			c.close()
			try:
				result = json.loads(data)
			except ValueError:
				return None
			if 'results' not in result:
				return None
			self.max_id = result['max_id']
			self.results = result['results']
			self.since_id = result['since_id']
			return result['results']
		except (httplib.HTTPException, socket.error, socket.timeout), e:
			print "search() error: %s" %(e)
			return None
 
	def loop(self):
		#while True:
		print "Starting search"
		data = self.search()
		if data:
			print "%d new result(s)" %len(data)
			for entry in data:
				self.parse(entry)
		else:
			print "No new results"
		print "Search complete waiting: %d seconds" %(self.interval)
		time.sleep(float(self.interval))
	def run(self):
		data = self.search()
		if data:
			for entry in data:
				self.parse(entry)
				#save to database
			return len(data)
		else:
			return -1
	

	def parse(self, entry):
		info = {}
		info['authorid'] = entry['id']
		info['authorname'] = entry['from_user'].encode('utf-8')
		info['content'] = entry['text'].encode('utf-8')
		info['url'] = entry['source'].encode('utf-8')
		info['source'] = "twitter"

		date = datetime.datetime.now().strftime("%y%j%H%M")
		#print info
		postVal,followers = self.ranker.calculate_value(info)


		if postVal != -1 and followers != -1:
			conn = sqlite3.connect(self.database)
			c = conn.cursor()
			data = (info['authorname'], 't', followers, date, postVal, hashlib.md5(info['content']).hexdigest(), entry['text'])
			print "T Putting", data

			c.execute('insert into userdata values (?,?,?,?,?,?,?)', data)
			conn.commit()
			c.close()

		

		#print info['authorname'], info['content'], postVal
		#pass

